package com.acme.bankapp.service.bank;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import com.acme.bankapp.domain.account.Account;
import com.acme.bankapp.domain.account.CheckingAccount;
import com.acme.bankapp.domain.account.SavingAccount;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.client.Client;
import com.acme.bankapp.domain.exceptions.InvalidFeedDataException;

public class BankDataLoaderService {
	private BankDataParser dataParser = new BankDataParser();
	
	public BankDataLoaderService() {
	}
	
	/**
	 * Adds to givven bank clients with their account from file, specified
	 * by <code>feedPath</code>
	 * @param bank
	 * @param feedPath
	 * @throws IOException
	 * @throws InvalidFeedDataException
	 */
	public void readBank(Bank bank, String feedPath) throws IOException, InvalidFeedDataException {
		FileReader fr = null;
		BufferedReader br = null;
		try {
			fr = new FileReader(feedPath);
			br = new BufferedReader(fr);
			readBank(br, bank);
		} catch (IOException e) {
			throw e;
		} finally {
			if (fr != null) {
				try {
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	public void readBank(BufferedReader reader, Bank bank) throws InvalidFeedDataException, IOException {
		String line = null;
		while ((line = reader.readLine()) != null) {
			bank.addClient(dataParser.readClientFromLine(line));
		}
	}
}
 